package ModelLayer;
import java.util.ArrayList;


/**
 * Write a description of class SaleCon here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SaleCon
{
    private static SaleCon instance;
    private ArrayList<Sale> saleCon;
    
    public static SaleCon getInstance()
    {
        if(instance == null)
        {
            instance = new SaleCon();
        }
        return instance;
    }
    
    private SaleCon()
    {
        saleCon = new ArrayList<Sale>();
    }
    
    public ArrayList getSaleCon()
    {
        return saleCon;
    }
    
    public Sale getSale(int id)
    {
        boolean found = false;
        int i = 0;
        Sale tempSale = null;
        while(i < saleCon.size() && !found)
        {
            tempSale = saleCon.get(i);
            if(tempSale.getId() == id)
            {
                found = true;
            }
            else
            {
                tempSale = null;
                i ++;
            }
        }
        return tempSale;
    }
    
    public void printSale(int id)
    {
        if(getSale(id) != null)
        {
            System.out.println("ID: "+getSale(id).getId());
            System.out.println("Registration date: "+getSale(id).getRegDate());
            System.out.println("Status: "+getSale(id).getStatus());
            System.out.println("Total price: "+getSale(id).getTotalPrice());
            if(getSale(id).getCustomer() == null)
            {
                System.out.println("Business customer: "+getSale(id).getBusiness().getName());
            }
            else
            {
                System.out.println("Customer :"+getSale(id).getCustomer().getName());
            }
            for(int p = 0; p < getSale(id).getPartsale().size(); p++)
            {
                int n = p+1;
                System.out.println("Item "+n+": "+getSale(id).getPartsale().get(p).getItem().getDescription()
                +", quantity: "+getSale(id).getPartsale().get(p).getQuantity());
            }
            System.out.println("");
        }
        else
        {
            System.out.println("Matching sale not found for id: "+id);
        }
    }
    
    public void printSaleList()
    {
        if(!saleCon.isEmpty())
        {
            System.out.println("List of sales: \n");
            for(int i = 0; i < saleCon.size(); i++)
            {
                System.out.println("ID: "+saleCon.get(i).getId());
                System.out.println("Registration date: "+saleCon.get(i).getRegDate());
                System.out.println("Status: "+saleCon.get(i).getStatus());
                System.out.println("Total price: "+saleCon.get(i).getTotalPrice());
                if(saleCon.get(i).getCustomer() == null)
                {
                    System.out.println("Business customer: "+saleCon.get(i).getBusiness().getName());
                }
                else
                {
                    System.out.println("Customer :"+saleCon.get(i).getCustomer().getName());
                }
                for(int p = 0; p < saleCon.get(i).getPartsale().size(); p++)
                {
                    int n = p+1;
                    System.out.println("Item "+n+": "+saleCon.get(i).getPartsale().get(p).getItem().getDescription()
                    +", Quantity: "+saleCon.get(i).getPartsale().get(p).getQuantity());
                }
                System.out.println("");
            }
        }
        else
        {
            System.out.println("There are currently no sales listed in the system");
        }
    }
    
    public void addSale(Sale sale)
    {
        saleCon.add(sale);
    }
    
    public void removeSale(int id)
    {
        Sale tempSale = getSale(id);
        if(tempSale != null)
        {
            saleCon.remove(tempSale);
        }
        else
        {
            System.out.println("Matching sale not found for id: "+id);
        }
    }
}
